﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;

namespace Kaixin
{
    using Kaixin.Tools;
    using Kaixin.House;
    using Kaixin.House.Garden;
    using Kaixin.House.Ranch;


    public class KaiXin001
    {
        #region Properties
        private string _verify = "";
        private string _uid = "";
        private Garden _MyGarden = null;
        private Network _network = new Network();

        // Garden
        private int _DelayGardenLoop = 10 * 1000 * 60;
        private int _DelayGardenEach = 2 * 1000;
        private int _DelayGardenFarm = 1 * 1000;
        private Thread _ThreadGardenAuto = null;
        
        private AntiTheive _Anti = null;
        private SeedList _Seeds = null;
        private MySeedList _MySeeds = null; // myself

        public event AntiTheiveTriggeredEvent AntiTheiveTriggered;


        // Ranch
        private Ranch _MyRanch = null;
        private Thread _ThreadRanchAuto = null;

        public FriendList FriendList;

        public string UID
        {
            get 
            {
                return _uid;
            }
        }
        #endregion

        #region Common Method
        public KaiXin001()
        {
        }

        // return value: 
        //  1: succ
        //  0: fail
        // -1: need code
        public int Login(string user, string pass)
        {
            //return -1;

            int ret = Loginout.Login(_network, user, pass, out _uid);
            if ( ret == 1)
                Initialize();
            return ret;
        }

        // return value: 
        //  1: succ
        //  0: fail
        public int LoginWithCode(string user, string pass, string code)
        { 
            int ret = Loginout.LoginWithCode(_network, user, pass, code, out _uid);
            if (ret == 1)
            {
                Initialize();
            }
            return ret;
        }

        public byte[] LoginGetCode()
        {
            return Loginout.GetCode(_network);
        }

        private void _Initialize()
        {
            // Garden
            _Anti = new AntiTheive(_network, _verify);
            _Anti.AntiTheiveTriggered += new AntiTheiveTriggeredEvent(_Anti_AntiTheiveTriggered);
            _MySeeds = new MySeedList(_network);
            _Seeds = new SeedList(_verify, _network);
            _Seeds.UpdateSeedList();
        }

        private void Initialize()
        {
            //_uid = Loginout.GetUid(_network);
            GetHouseVerifyCode();
            FriendList = new FriendList(_network, _uid, _verify);
            FriendList.UpdateFriendList();

            Thread _InitThread = new Thread(new ThreadStart(_Initialize));
            _InitThread.Start();
        }

        public void Logout()
        {
            StopGardenAuto();
            StopRanchAuto();

            _uid = "";
            _verify = "";

            _MySeeds = null;
            _Anti = null;
            FriendList = null;
            _MyGarden = null;

            Loginout.Logout(_network);
        }

        private bool GetHouseVerifyCode()
        {
            string url = Consts.RootURL + Consts.HouseUrl;

            try
            {
                string resp = _network.CallUrl(url);
                string pat = "g_verify = \"([^<]+?)\";";

                Match m = Regex.Match(resp, pat);
                if (m.Success)
                {
                    _verify = m.Groups[1].Value;
                    return true;
                }
                return false;
            }
            catch (Exception)
            {
                return false;
            }
        }

        #endregion

        #region Garden Related Method
        // ------------------------------ Garden begin -------------------------------------------------------
        public Garden GetGarden()
        {
            if (_MyGarden == null)
                //_MyGarden = new Garden(_uid, _uid, _verify, _network, _MySeeds, _Anti);
                _MyGarden = new Garden(_uid, FriendList.GetFriendByUid(_uid), _verify, _network, _MySeeds, _Anti);

            _MyGarden.UpdateInfo();

            return _MyGarden;
        }

        public Garden GetGarden(string fuid)
        {
            if (fuid.Equals(_uid))
                return GetGarden();
            else
            {
                //Garden garden = new Garden(_uid, fuid, _verify, _network, _MySeeds, _Anti);
                Garden garden = new Garden(_uid, FriendList.GetFriendByUid(fuid), _verify, _network, _MySeeds, _Anti);
                garden.UpdateInfo();
                return garden;
            }
        }

        public void Harvest()
        {
            Harvest(_uid);
        }

        public void Harvest(string fuid)
        {
            if (!fuid.Equals(""))
            {
                Garden garden = GetGarden(fuid);
                garden.DoAction(Garden.ActionType.Harvest);
            }
        }

        public void AntiGrass(string fuid)
        {
            if (!fuid.Equals(""))
            {
                Garden garden = GetGarden(fuid);
                garden.DoAction(Garden.ActionType.AntiGrass);
            }
        }

        public void AntiGrass()
        {
            AntiGrass(_uid);
        }

        public void Water(string fuid)
        {
            if (!fuid.Equals(""))
            {
                Garden garden = GetGarden(fuid);
                garden.DoAction(Garden.ActionType.Water);
            }
        }

        public void Water()
        {
            Water(_uid);
        }

        public void AntiVermin(string fuid)
        {
            if (!fuid.Equals(""))
            {
                Garden garden = GetGarden(fuid);
                garden.DoAction(Garden.ActionType.AntiVermin);
            }
        }

        public void Plough(string fuid)
        {
            if (!fuid.Equals(""))
            {
                Garden garden = GetGarden(fuid);
                garden.DoAction(Garden.ActionType.Plough);
            }
        }

        public void FarmSeedRandom(string fuid)
        {
            if (!fuid.Equals(""))
            {
                Garden garden = GetGarden(fuid);
                garden.DoAction(Garden.ActionType.FarmSeedRandom);
            }
        }

        public void StopGardenAuto()
        {
            if (_ThreadGardenAuto != null)
            {
                _ThreadGardenAuto.Abort();
                _ThreadGardenAuto = null;
            }
        }

        public void GardenDoAuto()
        {
            if (_ThreadGardenAuto == null)
            {
                _ThreadGardenAuto = new Thread(new ThreadStart(_GardenDoAuto));
                _ThreadGardenAuto.IsBackground = true;
                _ThreadGardenAuto.Start();
            }
        }

        private void _GardenDoAuto()
        {
            while (true)
            {
                for (int i = 0; i < FriendList.Items.Count; i++)
                {
                    Garden garden = GetGarden(FriendList.Items[i].Uid);
                    Garden.ActionType a;
                    for (a = Garden.ActionType.Harvest; a < Garden.ActionType.FarmSeed; a++)
                        garden.DoAction(a);

                    Thread.Sleep(_DelayGardenEach);
                }
                Thread.Sleep(_DelayGardenLoop);
            }
        }

        private void _Anti_AntiTheiveTriggered(object sender, EventArgs e)
        {
            if (AntiTheiveTriggered != null)
                AntiTheiveTriggered(_Anti, e);
        }
        // ------------------------------ Garden end -------------------------------------------------------
        #endregion

        #region Ranch Related Method
        //--------------------------------Ranch Begin ------------------------------------------------------

        public Ranch GetRanch()
        {
            if (_MyRanch == null)
                _MyRanch = new Ranch(_uid, FriendList.GetFriendByUid(_uid), _verify, _network);

            _MyRanch.UpdateInfo();

            return _MyRanch;
        }

        public Ranch GetRanch(string fuid)
        {
            if ("".Equals(fuid))
                return GetRanch();
            else
            {
                Ranch ranch = new Ranch(_uid, FriendList.GetFriendByUid(fuid), _verify, _network);
                ranch.UpdateInfo();
                return ranch;
            }
        }

        public void StopRanchAuto()
        {
            if (_ThreadRanchAuto != null)
            {
                _ThreadRanchAuto.Abort();
                _ThreadRanchAuto = null;
            }
        }

        public void RanchDoAuto()
        {
            if (_ThreadRanchAuto == null)
            {
                _ThreadRanchAuto = new Thread(new ThreadStart(_RanchDoAuto));
                _ThreadRanchAuto.IsBackground = true;
                _ThreadRanchAuto.Start();
            }
        }

        private void _RanchDoAuto()
        {
            while (true)
            {
                for (int i = 0; i < FriendList.Items.Count; i++)
                {
                    Ranch ranch = GetRanch(FriendList.Items[i].Uid);
                    Ranch.ActionType a;
                    for (a = Ranch.ActionType.HarvestProduct; a < Ranch.ActionType.Carrot; a++)
                        ranch.DoAction(a);

                    Thread.Sleep(_DelayGardenEach);
                }
                Thread.Sleep(_DelayGardenLoop);
            }
        }

        //--------------------------------Ranch end ------------------------------------------------------
        #endregion


    }
}
